<template>
  <div class="topMenu">
    <div class="container">
      <ImageLoad :filePath="props.logo" class="bannerImg"></ImageLoad>
      <div class="menu-container" v-if="showMainMenu">
        <div class="menu"
             :class="menu.isActive ? 'active': '' "
             v-for="(menu, index) in routeList"
             @click="goPath(menu.path, index)"
             :key="`menu_${index}`">{{menu.name}}
        </div>
        <div class="menu nickname"
             @click="goUserCenter"
             :class="$route.path === '/userCenter'? 'active' : ''"
             v-if="userInfo">{{userInfo.nickname}}</div>
        <div class="loginBtn" @click="router.push('/auth')" v-if="!isSign">登录/注册</div>
        <div class="logoutBtn" @click="logout" v-else>退出</div>
      </div>
      <div class="menu-container subMenu" v-else>
        <div class="menu rightAuto" @click="router.push('/')">首页</div>
        <div class="menu">中文</div>
        <div class="menu">English</div>
        <div class="menu">用户信息</div>
        <div class="logoutBtn" @click="router.push('/auth')">退出</div>
      </div>
    </div>
  </div>
</template>


<script setup>
import ImageLoad  from "@/components/common/ImageLoad/index.vue";

import { useRouter, useRoute } from 'vue-router'
const router = useRouter()
const $route = useRoute()
import { useEventStore } from '@/stores/event/index.js'
import { useUserStore } from '@/stores/user/index.js'
import { toLogout } from '@/api/common.js'
import { storageDict } from '@/config/dict.js'
const userStore = useUserStore()
const isSign = ref(userStore.isSign)
const props = defineProps({
  logo: {
    type: String,
    default: "topBanner.png"
  }
})
const showMainMenu = ref(true)
const userInfo = userStore.userInfo || localStorage.getItem(storageDict.USER_INFO);
const routeList = ref([
  {name: '首页', path: '/' , isActive: false},
  {name: '赛事信息', path: '/eventDetails' , isActive: false},
  {name: '新闻公告', path: '/seeMore' , isActive: false},
  {name: '报名', path: '/signUp' , isActive: false},
  {name: '成绩查询', path: '/query' , isActive: false},
  {name: '合作伙伴', path: '/partners' , isActive: false},
  {name: '志愿者', path: '/volentier' , isActive: false},
  {name: '中文', path: '/zh' , isActive: false},
  {name: 'English', path: '/english' , isActive: false},
])
const goPath = (path, index) => {
  routeList.value.forEach(item => {
    item.isActive = false
  })
  routeList.value[index].isActive = true
  router.push(path)
}
const goUserCenter = () => {
  routeList.value.forEach(item => {
    item.isActive = false
  })
  router.push('/userCenter?type=information')
}
const logout = () => {
  toLogout().then(res => {
    if (res.code === 200) {
      userStore.signOut()
      isSign.value = false
      router.push('/auth')
    }
  })
}
onMounted(() => {
  routeList.value.forEach(item => {
    item.isActive = item.path === $route.path;
  })
  if (userStore.isSign) {
    userStore.getUserInfo();
  }
})
</script>

<style scoped lang="scss">
.rightAuto {
  margin-right:auto;
}
.topMenu {
  //position: fixed;
  top: 0;
  left: 0;
  right: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  background: #fff;
  z-index: 99;
}
.bannerImg {
  width: 100%;
  height: 100px;
  object-fit: fill;
}
.menu-container {
  margin-top: 20px;
  display: flex;
  align-items: center;
  padding-bottom: 10px;
  gap: 46px;
  .menu {
    font-size: 18px;
    font-weight: 400;
    cursor: pointer;
    position: relative;
    &:hover {
      color: #5F3789;
      &::after {
        content: "";
        position: absolute;
        left: 0;
        width: 100%;
        height: 3px;
        border-radius: 1px;
        background: #5F3789;
        top: calc(100% + 10px);
      }
    }
  }
  .active {
    &::after {
      content: "";
      position: absolute;
      left: 0;
      width: 100%;
      height: 3px;
      border-radius: 1px;
      background: #5F3789;
      top: calc(100% + 10px);
    }
  }
  .loginBtn {
    margin-left: auto;
    background: #5F3789;
    border-radius: 6px;
    padding: 12px 16px;
    color: #ffffff;
    font-size: 14px;
    &:hover {
      opacity: 0.8;
      cursor: pointer;
    }
  }

  .logoutBtn {
    background: transparent;
    border: 1px solid #5F3789;
    border-radius: 6px;
    padding: 12px 16px;
    color: #5F3789;
    font-size: 14px;
    cursor: pointer;
    &:hover {
      background: #5F3789;
      color: #ffffff;
    }
  }
  .nickname {
    margin-left: auto;
  }
}
.subMenu {
  justify-content:flex-end;
  border-bottom: 1px solid #e8e8e8;
}
</style>
